var menuMap = new Array();
var urlMap = new Array();

//获取客户公司的识别码
function getCompanyCode() {
    $.ajax({

        url:"/company/getCode",
        type: "post",
        data:{
            "companyId":"2"
        },
        dataType:"json",
        success:function (data) {
            if (data.result == true){
                alert(data.data);
            }
            else {
                alert(data.msg);
            }
        }
    });
}

//绑定菜单
function menuAction(menuList) {
    var el = document.getElementById("menu").getElementsByTagName("li");
    for (var i=0;i<el.length;i++){
        console.log(el[i].id);
        try {
            menuMap[el[i].id] = menuList[i].urle;
            urlMap[menuList[i].urle] = el[i].id;
        }catch (e) {

        }
        // each(el[i].id.substring(el[i].id.length-1)-1,i);
        each(i);
    }
    console.log(menuMap);
    console.log("11111111111")
    $("#menu_userInfo").click();
    //js的闭包问题，这里必须新建一个函数传参才能正常获取i值
    function each(i) {
        try {  //这里会报找不到onclick属性，但是依旧可以点击，需要整改
            el[i].onclick = function () {getMainBoxByName(menuList[i].urle,menuList[i].name,el[i].id)};
        }catch (e) {}
    }
}

//显示时间
var date = new Date();
$("#showDate").empty();
$("#showDate").append(
    date.getFullYear() + " 年 " +
    date.getMonth() + " 月 " +
    date.getDay() + " 日 "
);

//根据需要加载模块
function getMainBoxByName(boxName,boxText,menuId) {
    var mainBox = $("#main-box");
    var oldBoxChild =  document.getElementById(boxName);
    // mainBox.height("1000px");
    clearMainBox();
    // 如果没有就加载
    if (oldBoxChild == null){
        // var newBoxChild=$('<div></div>');
        // newBoxChild.attr('id',boxName);
        if(!boxName){
            console.log("aaaaaaa");
            return;
        }
        loadPage(mainBox,boxName,menuId);
        loadLableBar(boxText,menuId.substring(5));
        hideLableBarItem(menuId.substring(5));
        // newBoxChild.load(boxName);
        // mainBox.append(newBoxChild);
    }
    else {
        hideLableBarItem(menuId.substring(5));
        show(oldBoxChild);
    }
    // mainBox.height($("#"+boxName+"").height());
}

//使元素隐藏
function hidden(object) {
    object.classList.remove("show");
    object.classList.add("hidden");

}

//使元素显现
function show(object) {
    object.classList.remove("hidden");
    object.classList.add("show");
}

//刷新中央框
function refreshPage() {
    var mainBox = $("#main-box");
    var pageName = mainBox.children(".show").attr("id");
    var str = new RegExp('batch_');
    if (str.test(pageName)){
        mainBox.children(".show").empty();
        mainBox.children(".show").load("/" + pageName.substring(6) + "/batch");
        return;
    }
    console.log(mainBox.children());
    mainBox.children(".show").empty();
    mainBox.children(".show").load("/" + pageName);
}

//清除中央框显示的内容和相应的标签栏
function closePage() {
    // var mainBox = $("#main-box");
    var page = $("#main-box").children(".show");
    $(document.getElementById(page.attr("id") +"-lable")).remove();
    $(page).remove();
    $("#menu_userInfo").click();
}

//在中央框中加载页面
function loadPage(object,pageName) {
    var newBoxChild=$('<div></div>');
    newBoxChild.attr('id',pageName);
    newBoxChild.attr("class","show")
    console.log(pageName)
    newBoxChild.load("/"+pageName);
    object.append(newBoxChild);
    console.log(object.children())
}

//加载标签栏
function loadLableBar(boxText,menuId) {
        var lableBarItem = $("" +
        "<li class=\"nav-item\" style=\"margin:0px 0px 0px 0px\" id=\""+menuId+"-lable\">\n" +
        "<div class=\"lable-bar-item-after\" style='width: 150px'>\n" +
        "<div class=\"nav-link icon-link form-inline\" style=\"font-size:15px;padding:8px;position:relative \">" +
        "<a>"+ boxText+"</a>\n" +
        "<img id='closeMainBox-"+menuId+"' onclick='' style='width:20px;margin-top:3px;position:absolute;right:8px; ' src='/static/images/menuimg/closeLable.png'>"+
        "</div>"+
        "</div>\n" +
        "<div style=\"border-radius: 10px;margin-left: -10px;width: 20px\"></div>\n" +
        "</li>");
    $("#lable-bar").append(lableBarItem);
    $(document.getElementById(menuId+"-lable")).click(function () {
            var id = $(this).attr("id")
            console.log("点击的对象的" + id)
            clearMainBox();
            hideLableBarItem(id.substring(0,id.length-6));
            show(document.getElementById(id.substring(0,id.length-6)));
            // $(document.getElementById(menuId)).click()
        }
    );
    $(document.getElementById("closeMainBox-"+menuId)).click(function () {
            var id = $(this).parent().parent().parent().attr("id");
            console.log("id"+id)
            $("#menu_userInfo").click();
            $(document.getElementById(id)).remove();
            $(document.getElementById(id.substring(0,id.length-6))).remove();
        }
    )

}

//将除了id=itemId之外的li改变颜色
function hideLableBarItem(itemId) {
    var itemList = $(document.getElementById(itemId+"-lable")).siblings();
    $(document.getElementById(itemId+"-lable")).children().css("background","#ffffff");
    console.log("itemId" + itemId)
    for (var i = 0; i < itemList.length; i++) {
        $(itemList[i]).children().css("background","#C5DAEC");
    }

}

//使main-Box中的元素隐藏
function clearMainBox() {
    var boxes = document.getElementById("main-box").children;
    for (var i = 0; i<boxes.length; i++){
        hidden(boxes[i]);
    }
}

//根据传过来的数据重写模态框得到内容
function showModal(title,body,define) {
    var titleDiv = document.getElementById("myModalLabel");
    var bodyDiv = document.getElementById("myModalBody");
    titleDiv.innerText = "";
    bodyDiv.innerText = "";
    titleDiv.innerText = title;
    bodyDiv.innerText = body;
    if (define){
        var defineButton = document.getElementById("defineButton");
        var closeButton = document.getElementById("closeButton");
        defineButton.style.display = "none";
        closeButton.style.color = "white"
        closeButton.style.backgroundColor = "#6640b2";
    }

}

//调用完成模态框后，实他恢复原样
function returnModal() {
    setTimeout(function(){
        var defineButton = document.getElementById("defineButton");
        var closeButton = document.getElementById("closeButton");
        defineButton.style.display = "";
        closeButton.style.backgroundColor = "white";
        closeButton.style.color = "black"
        closeButton.style.display = "";
    },500);
}
